<?php
	require('sign.doc.php');
	//打卡
	$app->post(
		'/sign/add',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['info']) && isset($data['gymid'])){
				$info=authcode($data['info'], 'DECODE', 'adaa');
				if($info){
					$info=explode(',',$info);
					$data['type']=$info[0];
					$data['userid']=$info[1];
					$data['usertype']=$info[2];
					$tb = $data['usertype'] == 1 ? 'tb_member' : 'tb_coach';
					$user = $db2->select(1, $tb, '*', array('tbid' => $data['userid']));
					if($data['type'] == 1){//签到
						$sign = $db2->select(1, 'tb_sign', 'dt', array(
							'userid' => $data['userid'],
							'usertype' => $data['usertype'],
							'type' => $data['type'],
							"#date_format(dt,'%Y-%m-%d')"=>'curdate()',
							'order' => 'dt desc'
						));
						//如果状态为离线，或者状态为在线，并且最后次打卡时间超过3小时，教练为12小时
						if($user['isonline'] == 0 || !$sign){
							$db2->insert('tb_sign', array(
								'userid' => $data['userid'],
								'usertype' => $data['usertype'],
								'gym_id' => $data['gymid'],
								'type' => $data['type'],
								'dt' => date('Y-m-d H:i:s')
							));
							$db2->update($tb, array('isonline' => 1), array('tbid' => $data['userid']));
							//增加10积分
							$issign=$db2->select(2,'tb_point','*',array("#date_format(dt,'%Y-%m-%d')"=>'curdate()','type'=>1,'userid'=>$data['userid'],'usertype'=>$data['usertype']));
							if($issign==0){
								pointIncrease(10, $data['userid'],$data['usertype'],1);
							}
						}else{
							$echo['error'] = "你已签到过，请不要重复签到";
						}
					}else{//签出
						$sign = $db2->select(1, 'tb_sign', 'dt', array(
							'userid' => $data['userid'],
							'usertype' => $data['usertype'],
							'type' => 1,
							"#date_format(dt,'%Y-%m-%d')"=>'curdate()',
							'order' => 'dt desc'
						));
						if($user['isonline'] == 1 && $sign){
							$db2->insert('tb_sign', array(
								'userid' => $data['userid'],
								'usertype' => $data['usertype'],
								'gym_id' => $data['gymid'],
								'type' => $data['type'],
								'dt' => date('Y-m-d H:i:s')
							));
							$db2->update($tb, array('isonline' => 0), array('tbid' => $data['userid']));
							$gym = $db2->select(1, 'tb_gym', '*', array('tbid' => $data['gymid']));
							$one = strtotime($sign['dt']);//开始时间 时间戳
							$tow = strtotime('now');//结束时间 时间戳
							$cle = $tow - $one; //得出时间戳差值
							$h = floor(($cle%(3600*24))/3600);
							$issign=$db2->select(2,'tb_sign','*',array("#date_format(dt,'%Y-%m-%d')"=>'curdate()','type'=>2,'userid'=>$data['userid'],'usertype'=>$data['usertype']));
							if($issign==1){
								pointIncrease($h, $data['userid'],$data['usertype'],2);
								$adaaid=getordernum("AD");
								$pay=array('type'=>0,'userid'=>$data['userid'],'usertype'=>$data['usertype'],'adaaid'=>$adaaid);
								if(date('H:i:s') >= $gym['peaktime'] && date('H:i:s') < $gym['offpeaktime']){
									$pay['tradeadaa']=0.5;
								}else{
									$pay['tradeadaa']=1;
								}
								creatadaa($pay);
								updateadaa($adaaid,0);
							}
						}else{
							$echo['error'] = "你还没有签到";
						}
					}
				}else{
					$echo['error'] = "二维码已超时，请刷新后重试";
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			$echo['type']=$data['type'];
			$echo['userid']=$data['userid'];
			$echo['usertype']=$data['usertype'];
			echo json_encode($echo);
		}
	);
	//健身房打卡记录
	$app->get(
		'/sign/gym/info',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['gymid'])){
				$data=$db2->select(0,'tb_sign','*',array("#date_format(dt,'%Y-%m-%d')"=>'curdate()','gym_id'=>$data['gymid'],'order'=>'dt desc'));
				$signid=array();
				foreach($data as $value){
					if(!in_array($value['tbid'],$signid)){
						if($value['usertype']==1){
							$type='member';
						}else{
							$type='coach';
						}
						$tb='tb_'.$type;
						$user=$db2->select(1,$tb,'*',array('tbid'=>$value['userid']));
						$temp['realname']=$user['realname'];
						//真实姓名、性别、真实头像地址、签到次数、签到时间、签出时间、当前状态（在线/离线）、类型（会员/教练）、VIP会员到期时间、教练等级
						$temp['sex']=$user['sex']==1?'男':'女';
						$temp['head']=file_exists('dofiles/'.$type.'/'.$user['tbid'].'/head/head.jpg') ? SITEROOTURL.'dofiles/'.$type.'/'.$user['tbid'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
						$temp['signnum']=$db2->select(2,'tb_sign','*',array('userid'=>$value['userid'],'usertype'=>$value['usertype'],'type'=>1));
						if($value['type']==1){
							$temp['signtime']=$value['dt'];
							$temp['outtime']='';
							if(time()-strtotime($value['dt'])>2*3600){
								$temp['state']='强制下线';
							}else{
								$temp['state']='在线';
							}
						}else{
							$sign=$db2->select(1,'tb_sign','*',array('userid'=>$value['userid'],'usertype'=>$value['usertype'],"#date_format(dt,'%Y-%m-%d')"=>'curdate()','type'=>1,'gym_id'=>$value['gym_id'],'order'=>'dt desc','dt[<]'=>$value['dt']));
							$signid[]=$sign['tbid'];
							$temp['signtime']=$sign['dt'];
							$temp['outtime']=$value['dt'];
							$temp['state']='已下线';
						}
						if($value['usertype']==1){
							$tb='tb_member';
							$temp['type']='会员';
							if(strtotime($user['privilege_dt'])>=time()){
								$temp['privilege_dt']=$user['privilege_dt'];
							}else{
								$temp['privilege_dt']='非VIP会员';
							}
						}else{
							$tb='tb_coach';
							$temp['type']='教练';
							switch($user['level']){
								case 0:$temp['level']='淘汰';break;
								case 1:$temp['level']='银牌';break;
								case 2:$temp['level']='金牌';break;
								case 3:$temp['level']='钻石';break;
							}
						}
						$echo['data'][]=$temp;
					}
					$echo['membernum']=$db2->select(2,'tb_sign','*',array('group'=>'userid','usertype'=>1,'gym_id'=>$value['gym_id'],"#date_format(dt,'%Y-%m-%d')"=>'curdate()'));
					$query='select count(*) from (select * from tb_sign  where usertype = 1 and gym_id = '.$value['gym_id'].' and DATE_FORMAT(dt, "%Y-%m-%d") = CURDATE() and TIMESTAMPDIFF(HOUR, dt, NOW()) < CASE usertype WHEN 1 THEN 2 ELSE 12 END group by userid having mod(count(*), 2) = "1"  ) a where type!=2';
					$echo['memberonlinenum']=$db2->query($query)->fetchColumn();
					$echo['coachnum']=$db2->select(2,'tb_sign','*',array('group'=>'userid','usertype'=>2,'gym_id'=>$value['gym_id'],"#date_format(dt,'%Y-%m-%d')"=>'curdate()'));	
					$query='select count(*) from (select * from tb_sign  where usertype = 2 and gym_id = '.$value['gym_id'].' and DATE_FORMAT(dt, "%Y-%m-%d") = CURDATE() and TIMESTAMPDIFF(HOUR, dt, NOW()) < CASE usertype WHEN 1 THEN 2 ELSE 12 END group by userid having mod(count(*), 2) = "1"  ) a where type!=2';
					$echo['coachonlinenum']=$db2->query($query)->fetchColumn();
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
	//用户打卡记录
	$app->get(
		'/sign/info',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			$echo['data']="";
			if($data && isset($data['userid']) && isset($data['usertype']) && isset($data['month'])){
				$tb = $data['usertype'] == 1 ? 'tb_member' : 'tb_coach';
				$echo['data']=$db2->select(3,'tb_sign','#date_format(dt,"%Y-%m-%d")[dt]',array("date_format(dt,'%Y-%m')"=>$data['month'],'group'=>'date_format(dt,"%Y-%m-%d")','userid'=>$data['userid'],'usertype'=>$data['usertype']));
				if(!is_array($echo['data']) && !empty($echo['data'])){
					$echo['data']=array($echo['data']);
				}
			}else{
				$echo['error'] = "数据缺失，请检查";
			}
			echo json_encode($echo);
		}
	);
?>